package com.practice.car.cardataapp.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.car.cardataapp.entity.SystemManager;
import com.practice.car.cardataapp.entity.Token;
import com.practice.car.cardataapp.mapper.SystemManagerDao;
import com.practice.car.cardataapp.service.SystemManagerService;

import com.practice.car.cardataapp.util.R;
import com.practice.car.cardataapp.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * car-data-app project
 *
 * @author 陈春幸
 * @date 2020/9/5 0005
 */
@Service("SystemManagerService")
public class SystemManagerServiceImpl extends ServiceImpl<SystemManagerDao,SystemManager> implements SystemManagerService {


	@Override
	public SystemManager findSystemManager(String name, String password) {

		SystemManager systemManager = this.getById(name);
		String encryptedPassword = encryptPassword(password);
		if (systemManager != null && systemManager.getPassword().equals(encryptedPassword)) {
			return systemManager;
		}
		return null;

	}

	@Override
	public R makeR(String name, String password) {

		SystemManager systemManager = this.findSystemManager(name, password);
		if (systemManager == null) {
			return R.create().error("用户不存在或密码错误！");
		}
		String smRole;
		smRole = systemManager.getRole();
		Token token = new Token();
		token.setMsg(name + "-" + smRole);
		token.setExpireTime(String.valueOf(System.currentTimeMillis() + TokenUtil.getExpireTime()));
		return TokenUtil.getRWithToken(token);
	}

	@Override
	public String encryptPassword(String password) {
		return SecureUtil.md5(password);
	}

}
